package com.zhyaoqi.platformautoplug.service;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.GestureDescription;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.graphics.Path;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.blankj.utilcode.util.ScreenUtils;
import com.zhyaoqi.platformautoplug.activity.SettingActivity;
import com.zhyaoqi.platformautoplug.app.MyApplication;
import com.zhyaoqi.platformautoplug.util.LogUtils;
import com.zhyaoqi.platformautoplug.util.SpUtils;

import java.util.List;
import java.util.Random;

public class PlugService extends AccessibilityService {

    private AccessibilityNodeInfo rootNodeInfo;


    @Override
    public void onCreate() {
        super.onCreate();
        LogUtils.logGGQ("---onCreate----");


    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {

        rootNodeInfo = getRootInActiveWindow();
        if (rootNodeInfo == null){
            return;
        }



        int eventType = event.getEventType();
        switch (eventType) {
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                LogUtils.logGGQ("界面点击");

                //界面点击
                break;
            case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED:
                LogUtils.logGGQ("界面文字改动");
                //界面文字改动
                break;
            case AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT: //文本改变
                LogUtils.logGGQ("文本改变");
                break;
            case AccessibilityEvent. TYPE_VIEW_HOVER_EXIT: //
                LogUtils.logGGQ("最后一个窗口");
                break;
            case AccessibilityEvent.TYPE_VIEW_SCROLLED:
                LogUtils.logGGQ("View滚动");
                break;
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                LogUtils.logGGQ( "打开了一个PopupWindow，Menu或Dialog");
                break;

            case AccessibilityEvent.TYPE_VIEW_SELECTED:
                LogUtils.logGGQ( " View被选中");
                break;
            case AccessibilityEvent.TYPE_VIEW_FOCUSED:
                LogUtils.logGGQ( " View获得焦点");
                break;
            case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
                LogUtils.logGGQ( " 开始手势监测");
                break;



            case AccessibilityEvent.TYPE_GESTURE_DETECTION_END:
                LogUtils.logGGQ( " 结束手势监测");
                break;



            case AccessibilityEvent.TYPE_TOUCH_INTERACTION_START:
                LogUtils.logGGQ( " 触摸屏幕事件开始");
                break;


            case AccessibilityEvent.TYPE_TOUCH_INTERACTION_END:
                LogUtils.logGGQ( " 触摸屏幕事件结束");
                break;


            case AccessibilityEvent.TYPE_ASSIST_READING_CONTEXT:
                LogUtils.logGGQ( " 辅助用户读取当前屏幕事件");
                break;


            case AccessibilityEvent.TYPE_VIEW_CONTEXT_CLICKED:
                LogUtils.logGGQ( " View中的上下文点击事件");
                break;




        }



        String selectPlatformArr = SpUtils.getString(SettingActivity.SELECT_PLATFORM_ARRAY,"");
        if (selectPlatformArr.contains("快手极速版")){
            try {
                kuaishou(rootNodeInfo);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (selectPlatformArr.contains("抖音")){
            douyin(rootNodeInfo);
        }






    }


    @Override
    public void onInterrupt() {

    }

    @Override
    public void onServiceConnected() {
        super.onServiceConnected();
        Toast.makeText(this,"连接启动成功",Toast.LENGTH_LONG).show();
    }


    private void kuaishou(AccessibilityNodeInfo rootNodeInfo) throws InterruptedException {
        String kuaishouPackage = "com.kuaishou.nebula";
        if (!kuaishouPackage.equals(rootNodeInfo.getPackageName())){
            return;
        }

        List<AccessibilityNodeInfo> tabsNodes = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.kuaishou.nebula:id/tabs");
//
        if (checkNotEmpty(tabsNodes)) {
            AccessibilityNodeInfo childNode = tabsNodes.get(0);
            AccessibilityNodeInfo faxianNode = childNode.getChild(2);//找到发现这个节点
            long t = 12000+new Random().nextInt(9800);
            Thread.sleep(800);
            Toast.makeText(getApplicationContext(), ""+t+"秒", Toast.LENGTH_LONG).show();
            faxianNode.performAction(AccessibilityNodeInfo.ACTION_CLICK);

        }


        List<AccessibilityNodeInfo> infos1_like = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.kuaishou.nebula:id/like_button");

        for (AccessibilityNodeInfo item2 : infos1_like) {

            int number = new Random().nextInt(10) + 1;

            if (number<3){
                item2.performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }

        }








    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void douyin(AccessibilityNodeInfo rootNodeInfo){




        String douyinPackage = "com.ss.android.ugc.aweme";



        if (!douyinPackage.equals(rootNodeInfo.getPackageName())){
            return;
        }


        ClipboardManager clipboard1 = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData clip1 = ClipData.newPlainText("label", "https://v.douyin.com/J6qfQC6/");
        clipboard1.setPrimaryClip(clip1);
        MyApplication.getInstance().startApp("com.ss.android.ugc.aweme");








     /*   *//**
         * 模拟上滑操作
         *//*

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
*/



        /*List<AccessibilityNodeInfo> ae3_tuijian = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/h1k");


        for (AccessibilityNodeInfo item : ae3_tuijian) {
            long t = 12000 + new Random().nextInt(9800);
            try {
                Thread.sleep(1000);

            } catch (Exception ex) {
                ex.printStackTrace();
            }

            *//**
             * 模拟下滑操作
             *//*

         *//*   performGlobalAction(GLOBAL_ACTION_NOTIFICATIONS);*//*




       *//*     performGlobalAction(AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);

            item.performAction(AccessibilityNodeInfo.ACTION_CLICK);*//*
      *//*      item.performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);*//*
*/
/*

        }
*/


            try {
                /*//仿滑动
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
                        Path path = new Path();
                        path.moveTo(540, 2000);//滑动起点
                        Thread.sleep(2500);
                        path.lineTo(540, 100);//滑动终点
                        Thread.sleep(4000);
                        GestureDescription.Builder builder = new GestureDescription.Builder();
                        GestureDescription description = builder.addStroke(new GestureDescription.StrokeDescription(path, 100L, 100L)).build();
                        //100L 第一个是开始的时间，第二个是持续时间
                        dispatchGesture(description, new MyCallBack(), null);
                    }
*/


                              //根據鏈接找人
                //粘贴板

              /*  ClipboardManager clipboard1 = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                ClipData clip1 = ClipData.newPlainText("label", "https://v.douyin.com/J6qfQC6/");
                clipboard1.setPrimaryClip(clip1);
                MyApplication.getInstance().startApp("com.ss.android.ugc.aweme");

                List<AccessibilityNodeInfo> infos_search = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/eij");
                for (AccessibilityNodeInfo item_search : infos_search){
                    item_search.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }

                List<AccessibilityNodeInfo> infos_ks = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/ks");
                for (AccessibilityNodeInfo item_ks : infos_ks){
                    item_ks.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                }*/


               /* List<AccessibilityNodeInfo> infos_dianzan = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/atg");


                for (AccessibilityNodeInfo item : infos_dianzan) {

                    int number = new Random().nextInt(10) + 1;

                    if (number>5){

                        item.performAction(AccessibilityNodeInfo.ACTION_CLICK);

                    }

                }*/




/*

                List<AccessibilityNodeInfo> infos_pl = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/aeo");


                for (AccessibilityNodeInfo item : infos_pl) {

                    int number = new Random().nextInt(10) + 1;

                    if (number>5){

                        item.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        Thread.sleep(1580);

                        List<AccessibilityNodeInfo> infos_pl2 = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/adq");



                           for (AccessibilityNodeInfo item1 : infos_pl2) {
                               LogUtils.logGGQ("item1:" + item1.getViewIdResourceName());
                              // Thread.sleep(1380);
                               String txt ="大家百事可乐万事芬达，心情雪碧，一周七喜，天天娃哈哈，天天挣大钱，天天发大财啊";
                               //粘贴板
                               ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                               ClipData clip = ClipData.newPlainText("label", txt);
                               clipboard.setPrimaryClip(clip);

                               Bundle bundle=new Bundle();
                               bundle.putCharSequence(AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,txt);
                               Thread.sleep(3380);
                                 item1.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT,bundle);
                               Thread.sleep(3380);


                               List<AccessibilityNodeInfo> infos_fs3 = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/ae9");
                               for (AccessibilityNodeInfo item3 : infos_fs3) {

                                   item3.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                   Thread.sleep(3380);

                                   List<AccessibilityNodeInfo> infos_guanbi = rootNodeInfo.findAccessibilityNodeInfosByViewId("com.ss.android.ugc.aweme:id/ks");
                                   for (AccessibilityNodeInfo item4 : infos_guanbi) {
                                       item4.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                       Thread.sleep(1380);

                                   }
                                   }




                           }

                           }

                    }*/
                      //根據鏈接








            } catch (Exception e) {
                e.printStackTrace();
            }


        }








   /* }*/






    private boolean checkNotEmpty(List list){
        return list!=null&& list.size()>0;
    }


    @RequiresApi(api = Build.VERSION_CODES.N)
    private class MyCallBack extends GestureResultCallback {

        public MyCallBack() {
            super();
        }

        @Override
        public void onCompleted(GestureDescription gestureDescription) {
            super.onCompleted(gestureDescription);

        }

        @Override
        public void onCancelled(GestureDescription gestureDescription) {
            super.onCancelled(gestureDescription);

        }
    }
}
